/*
 * Middleware
 * Copyright (C) 2010/11
 * Riccardo Pulvirenti, Giuseppe Ravidà, Andrea Tino
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANDABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses
 */

/*!
 * File: manager.hpp
 */

#ifndef _MANAGER_HPP_
#define _MANAGER_HPP_

#include <iostream>
#include <string>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/bind.hpp>

#include "runner.hpp"
#include "workflow.hpp"
#include "synqueue.cpp"

using namespace middleware::queueing;

//-----------------------------------------------------------------------------
// Enum, struct, aliases
namespace middleware {
typedef SynQueue<Workflow, 25> WorkflowSynQueue;
/*!
 * Configuration for manager.
 */
typedef struct {
	std::string man_ip_toui_bind; /* IP not used to bind to for UIs */
	std::string man_port_toui_bind; /* Port to bind to, to listen for UIs */
	std::string man_ip_todisc; /* IP of discovery */
	std::string man_port_todisc; /* Port of discovery */
} ManagerConfig;
}
// Constants
//-----------------------------------------------------------------------------
namespace middleware {
/*!
 * Min port to which the Manager will listen for incoming requests.
 */
const unsigned int kManagerMinManPort = 1051;
/*!
 * Limiting port number to 2^16 - 1.
 */
const unsigned int kManagerMaxManPort = 65536 - 1;
/*!
 * Etiquette for output.
 */
const std::string kManagerEtq = "MAN: ";
const std::string kManager1Etq = "WFEnqueuer: ";
}
//-----------------------------------------------------------------------------
// Class definitions
namespace middleware {
/*!
 * Implements elements necessary for UI component to run.
 */
class Manager : public Runner {
public:
	/*!
	 * Constructs a new Manager listening to loopback address and casual port
	 * > kMinManPort. Discovery address is set to loopback and port is invalid.
	 */
	Manager();
	/*!
	 * Constructs a new Manager with passed configuration.
	 */
	Manager(ManagerConfig conf);
	/*!
	 * Destroy Manager.
	 */
	~Manager();
	/*!
	 * Runs Manager. If another thread was running, it will be destoyed.
	 */
	void run() {
		this->_manager_thread.reset();
		this->_manager_thread = boost::shared_ptr<boost::thread>(
				new boost::thread(
						boost::bind(&Manager::exec, this)));
	}
	/*!
	 * Join this thread and wait for it to finish. When calling run, user
	 * should immediately call join in order to let the current thread
	 * not to die and terminate child.
	 */
	void join() {
		(this->_manager_thread)->join();
	}
private:
	/*!
	 * Creates a casual port number > 1050 and gets it.
	 */
	std::string createPort();
	/*!
	 * Execution function.
	 */
	void exec();
	/*!
	 * Execution function.
	 */
	void exec_enqueuer();
	/*!
	 * After that Manager gets a workflow, it must deal with it in order to
	 * route it to the correct workers. This is a thread execution flow.
	 */
	void ManageWorkflow(Workflow workflow);
	/*!
	 * Initialization routine with user.
	 */
	void bonjour();
	/*!
	 * Configuration.
	 */
	ManagerConfig _config;
	/*!
	 * Undrlaying thread. This thread is responsible for managing Manager
	 * activity, it also dequeues elements from the main queue.
	 */
	boost::shared_ptr<boost::thread> _manager_thread;
	/*!
	 * This thread is responsible for enqueueing elements when requested by
	 * an external connecting entity.
	 */
	boost::shared_ptr<boost::thread> _enqueuer_thread;
	/*!
	 * Shared queue of workflows.
	 */
	WorkflowSynQueue _wf_synqueue;
}; /* UI */
} /* middleware */

#endif
